import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {NzMessageService} from "ng-zorro-antd/message";
import {ValidatorsService} from "../../../../core/services/validators/validators.service";
import {AdminService} from "../../../../core/services/store/admin.service";
import {BasicInformationService} from "../../../../core/services/http/classification-identification/basic-information.service";
import {IdentifyIdService} from "../../../../core/services/store/biz-store-service/identify-id/identify-id.service";
import {fnCheckForm} from "../../../../utils/tools";


@Component({
  selector: 'app-basic-information',
  templateUrl: './basic-information.component.html',
  styleUrls: ['./basic-information.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class BasicInformationComponent implements OnInit {
  validateForm!: FormGroup;
  formOrder = 1;
  id!: number;

  constructor(private fb: FormBuilder, private msg: NzMessageService,
              private validatorsService: ValidatorsService,
              private cdr: ChangeDetectorRef,
              private identifyIdService: IdentifyIdService, public AdminService: AdminService,
              private dataService: BasicInformationService) {
  }

  initForm(): void {
    this.validateForm = this.fb.group({
      cnName: [null, [Validators.required]],
      normalState: [null],
      un: [null],
      dangerNo: [null, [Validators.required]],
      alias: [null],
      cas: [null],
    });
  }

  submitForm(): void {
    if (!fnCheckForm(this.validateForm)) {
      return;
    }
    const params = this.validateForm.value;
    if (this.id === -1) {
      this.dataService.addBasicInformation(this.validateForm.value).subscribe((res) => {
        // @ts-ignore
        this.identifyIdService.setIdentifyIdStore(res);
        this.cdr.markForCheck();
      });
    } else {
      params.id = this.id;
      this.dataService.editBasicInformation(params).subscribe(() => {
      })
    }
  }


  getDetail(id: number): void {
    this.dataService.getBasicInformationDetail(id).subscribe(res => {
      this.validateForm.patchValue(res);
    })
  }

  ngOnInit(): void {
    this.initForm();
    this.identifyIdService.getIdentifyIdStore().subscribe(res => this.id = res);
    if (this.id !== -1) {
      this.getDetail(this.id);
    }
  }

}
